﻿using Dapper;
using System;
using System.Linq;
using System.Text;
using Ayatta.Contract;
using Ayatta.Contract.Domain;
using System.Collections.Generic;

namespace Ayatta.Service
{
    public class ExtraService : BaseService, IExtraService
    {

        #region AuthSms

        public bool AuthSmsCreate(AuthSms o)
        {
            var template = new SqlBuilder()
            .Insert(Database.Extra.Table.AuthSms)
            .Values(o.Id, o.Group, o.Mobile, o.UserId, o.Code,o.Status, o.CreatedBy, o.CreatedOn)
            .Complete();

            using (var connection = GetConnection(ConnectionName.Extra).EnsureOpen())
            {
                return connection.Execute(template.Sql, template.Param) == 1;
            }
        }

        public void AuthSmsStatusUpdate(string id)
        {
            var template = new SqlBuilder()
                .Update(Database.Extra.Table.AuthSms)
                .Set("[Status]=1")
                .Where("[Id]={0}", id).Complete();

            using (var connection = GetConnection(ConnectionName.Extra).EnsureOpen())
            {
                connection.Execute(template.Sql, template.Param);
            }
        }

        public AuthSms AuthSmsGet(string id, string mobile, int? group = null, string code = null)
        {
            var template = new SqlBuilder()
                .Select("*")
                .From(Database.Extra.Table.AuthSms)
                .Where("[Id]={0} AND [Mobile]={1} AND [CreatedOn]>dateadd(minute,-30,getdate())", id, mobile)
                .If(group.HasValue && group.Value >= 0, "[Group]={0}", group)
                .If(!string.IsNullOrEmpty(code), "[Code]={0}", code).Complete();

            using (var connection = GetConnection(ConnectionName.Extra).EnsureOpen())
            {
                return connection.Query<AuthSms>(template.Sql, template.Param).FirstOrDefault();
            }
        }

        #endregion

        #region AuthEmail

        public bool AuthEmailCreate(AuthEmail o)
        {
            var template = new SqlBuilder()
            .Insert(Database.Extra.Table.AuthEmail)
            .Values(o.Id, o.Group, o.Email, o.UserId, o.Status, o.CreatedBy, o.CreatedOn)
            .Complete();

            using (var connection = GetConnection(ConnectionName.Extra).EnsureOpen())
            {
                return connection.Execute(template.Sql, template.Param) == 1;
            }
        }

        public void AuthEmailStatusUpdate(string id)
        {
            var template = new SqlBuilder()
                .Update(Database.Extra.Table.AuthEmail)
                .Set("[Status]=1")
                .Where("[Id]={0}", id).Complete();

            using (var connection = GetConnection(ConnectionName.Extra).EnsureOpen())
            {
                connection.Execute(template.Sql, template.Param);
            }
        }

        public AuthEmail AuthEmailGet(string id, int? group = null)
        {
            var template = new SqlBuilder()
                .Select("*")
                .From(Database.Extra.Table.AuthEmail)
                .Where("[Id]={0} AND [CreatedOn]>dateadd(minute,-30,getdate())", id)
                .If(group.HasValue && group.Value >= 0, "[Group]={0}", group).Complete();

            using (var connection = GetConnection(ConnectionName.Extra).EnsureOpen())
            {
                return connection.Query<AuthEmail>(template.Sql, template.Param).FirstOrDefault();
            }
        }

        #endregion

        #region IService
        public void CachePurge()
        {
            PurgeCache();
        }

        public PagedList<Magic<string, string, int>> CacheGet(int page = 1, int size = 50, int hitCount = 0)
        {
            return GetPagedCache(page, size, hitCount);
        }
        #endregion
    }
}
